package com.ld.poetry.dao;

import com.ld.poetry.entity.Article;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ld.poetry.vo.ArticleVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 文章表 Mapper 接口
 * </p>
 *
 * @author sara
 * @since 2021-08-13
 */
public interface ArticleMapper extends BaseMapper<Article> {

    @Update("update article set view_count=view_count+1 where id=#{id}")
    int updateViewCount(@Param("id") Integer id);

    @Select("SELECT * FROM article WHERE DATE(create_time) = #{date} AND deleted = 0 AND user_id = #{userid}")
    List<ArticleVO> selectDiariesByDate(@Param("date") String date, @Param("userid") Integer userid);

    @Select("SELECT DISTINCT a.* FROM article a " +
            "LEFT JOIN im_chat_user_friend f ON (a.user_id = f.friend_id OR a.user_id = f.user_id) " +
            "WHERE DATE(a.create_time) = #{date} AND a.deleted = 0 " +
            "AND (a.user_id = #{userid} OR (f.user_id = #{userid} OR f.friend_id = #{userid}) AND f.friend_status = 1)")
    List<ArticleVO> selectDiariesByDateWithFriend(@Param("date") String date, @Param("userid") Integer userid);

    @Select("SELECT DISTINCT a.* FROM article a " +
            "LEFT JOIN im_chat_user_friend f ON (a.user_id = f.friend_id OR a.user_id = f.user_id) " +
            "WHERE a.deleted = 0 " +
            "AND (a.user_id = #{userid} OR (f.user_id = #{userid} OR f.friend_id = #{userid}) AND f.friend_status = 1) " +
            "ORDER BY a.create_time DESC")
    List<ArticleVO> selectAllDiariesWithFriend(@Param("userid") Integer userid);
}
